import zrender from 'zrender'

// 插入图像
export const createBackground = (url, width, height) => {
  let backImg = new zrender.Image({
    zlevel: 0,
    style: {
      image: url,
      x: 0,
      y: 0,
      width: width,
      height: height
    }
  })
  return backImg
}

// 插入图像
export const createDot = (text, x, y) => {
  let dot = new zrender.Circle({
    zlevel: 0,
    style: {
      text: text
    },
    shape: {
      cx: x,
      cy: y,
      r: 10
    }
  })
  return dot
}

// 插入图标
export const createImg = (cate, url) => {
  let Group = new zrender.Group()
  // let cateTxtEn = url.split('/')[3].split('.')[0]
  cate.forEach(obj => {
    let x = obj.x
    let y = obj.y
    // 定义图标
    let objIcon = new zrender.Image({
      zlevel: 2,
      scale: [1, 1],
      style: {
        image: url,
        x: x,
        y: y,
        width: 26,
        height: 26,
        opacity: 1
      }
      // onclick () {
      //   if (cateTxtEn === 'camera_a') {
      //     store.commit('floor/setVideoUrl', obj.url)
      //     store.commit('floor/setVideoVisible', true)
      //   } else {
      //     store.commit('floor/setDeviceInfo', { name: obj.equipmentName, number: obj.equipmentLocalId, status: obj.status, type: obj.type, already: obj.already, overflowNum: obj.overflowNum })
      //     store.commit('floor/setDeviceVisible', true)
      //     if (cateTxtEn === 'elevator_a') {
      //       store.commit('floor/setElevatorVisible')
      //     }
      //   }
      // }
    })
    let circle = new zrender.Circle({
      zlevel: 1,
      shape: {
        cx: x + 13,
        cy: y + 13,
        r: 15
      },
      style: {
        fill: 'rgb(123,104,238)',
        opacity: 1
      }
    })
    // 报警的颜色变红
    // if (obj.status === 2 || obj.already === 0 || obj.status === 3) {
    //   circle.attr('style', {
    //     fill: '#ff0000'
    //   })
    // }
    Group.add(objIcon)
    Group.add(circle)
  })
  return Group
}

// 点位转换
export const dotTransfer = (x, y, lastWidth, lastHeight, nowWidth, nowHeight) => {
  const widthScale = (nowWidth / lastWidth).toFixed(2)
  const heightScale = (nowHeight / lastHeight).toFixed(2)
  let newX = (widthScale * x).toFixed(1)
  let newY = (heightScale * y).toFixed(1)
  return { newX, newY }
}
